package cc.cc4414.cc02.system.controller;

import java.util.List;

import javax.validation.constraints.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cc.cc4414.cc02.core.aspect.LogAspect;
import cc.cc4414.cc02.core.constant.RegexpConsts;
import cc.cc4414.cc02.core.controller.BaseController;
import cc.cc4414.cc02.core.result.ResultEnum;
import cc.cc4414.cc02.core.result.ResultVO;
import cc.cc4414.cc02.system.entity.Authority;
import cc.cc4414.cc02.system.service.AuthorityService;

/**
 * @author cc 2018年7月17日
 */
@RestController
@RequestMapping("/authority")
public class AuthorityController extends BaseController {

	@Autowired
	private AuthorityService authorityService;

	/**
	 * 查询所有权限信息
	 * 
	 * @return 所有权限
	 */
	@GetMapping("all")
	public ResultVO<List<Authority>> all() {
		return new ResultVO<List<Authority>>(authorityService.findAll());
	}

	/**
	 * 新增权限
	 * 
	 * @param name
	 *            权限名称
	 * @param description
	 *            描述
	 * @return
	 */
	@PostMapping("add")
	@LogAspect("新增权限")
	public ResultVO<Object> add(@Pattern(regexp = RegexpConsts.AUTHORITY_NAME) @RequestParam String name,
			@Pattern(regexp = RegexpConsts.AUTHORITY_DESCRIPTION) @RequestParam String description) {
		Authority authority = new Authority();
		authority.setName(name);
		authority.setDescription(description);
		authorityService.save(authority);
		return new ResultVO<>(ResultEnum.SUCCESS);
	}

	/**
	 * 检查权限是否存在
	 * 
	 * @param name
	 *            权限名
	 * @return 权限存在为true，权限不存在为false
	 */
	@GetMapping("checkAuthority")
	public ResultVO<Boolean> checkAuthority(@Pattern(regexp = RegexpConsts.AUTHORITY_NAME) @RequestParam String name) {
		ResultVO<Boolean> resultVO = new ResultVO<Boolean>(ResultEnum.SUCCESS);
		Authority authority = authorityService.findByName(name);
		if (authority != null) {
			resultVO.setData(true);
		} else {
			resultVO.setData(false);
		}
		return resultVO;
	}

	/**
	 * 通过权限id删除权限
	 * 
	 * @param id
	 *            权限id
	 * @return 是否删除成功
	 */
	@PostMapping("delete")
	@LogAspect("删除权限")
	public ResultVO<Object> delete(@RequestParam Long id) {
		authorityService.delete(id);
		return new ResultVO<>(ResultEnum.SUCCESS);
	}

	/**
	 * 修改描述
	 * 
	 * @param id
	 *            权限id
	 * @param description
	 *            修改后的描述
	 * @return 是否修改成功
	 */
	@PostMapping("updateDescription")
	@LogAspect("修改权限描述")
	public ResultVO<Object> updateDescription(@RequestParam Long id,
			@Pattern(regexp = RegexpConsts.AUTHORITY_DESCRIPTION) @RequestParam String description) {
		authorityService.updateDescription(id, description);
		return new ResultVO<>(ResultEnum.SUCCESS);
	}
}
